<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('indexApp.php');

class DealController extends IndexAppController {
	
	public function __construct() {
        parent::__construct();
        $this->dealLogic = new DealLogic();
	}
 	
 	public function actionDefault(){	
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $catid = intval($_REQUEST["id"]) ? $_REQUEST["id"] : 1;
        $size = 5;
        $cond="STATE = 1 AND DEAL_STAT IN (2,3,4,5,6)"; //前台显示与投资进行中
        if(intval($_REQUEST["time"])!=""){
            switch (intval($_REQUEST["time"])){
            case 0:break;
            case 1:$cond .= " and REPAY_TIME between 3 and 6 ";break;
            case 2:$cond .= " and REPAY_TIME between 9 and 15 ";break;
            case 3:$cond .= " and REPAY_TIME between 28 and 24 ";break;
            case 4:$cond .= " and REPAY_TIME >= 24";break;
            }
            T::$tpl->set("time", $time);
        }
        if(intval($_REQUEST["rate"])!=""){
            switch (intval($_REQUEST["rate"])){
            case 0:break;
            case 1:$cond .= " and RATE between 0 and 8 ";break;
            case 2:$cond .= " and RATE between 10 and 15 ";break;
            case 3:$cond .= " and RATE between 15 and 17 ";break;
            case 4:$cond .= " and RATE between 17 and 20";break;
            }
            T::$tpl->set("rate", $rate);
        }
        if(intval($_REQUEST["price"])!=""){
            switch (intval($_REQUEST["price"])){
            case 0:break;
            case 1:$cond .= " and PRICE between 0 and 50000 ";break;
            case 2:$cond .= " and PRICE between 50000 and 100000 ";break;
            case 3:$cond .= " and PRICE between 100000 and 150000 ";break;
            case 4:$cond .= " and PRICE between 150000 and 500000";break;
            }
           T::$tpl->set("price", $price);
        }
        $res = array();
        try{
            $res = $this->dealLogic->getDealList("*", $cond, "order by id desc", $page, $size);
            if($res['data']){
                foreach($res['data'] as $k=> $v){
                    $res['data'][$k]['stime'] = strtotime($v['BAD_TIME']) - time();      //倒计时
                    $res['data'][$k]['loading'] = round(($v['PRICE_LOAD'] / $v['PRICE_ALLOW'])*100);  //进度
                }
            }
            $par = array ();
            
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        }catch(Exception $e){
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
        //公告
        $newlist="";
        $artLogic = new ArticleLogic();
        $newlist = $artLogic->getArtList("*", "CATID = 7", "order by ID desc", 1, 4);
        T::$tpl->set('newlist', $newlist);
        
        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
 	}
    public function actionBid(){    
        $id = intval($_REQUEST['id']);
        $res = $this->dealLogic->getDealItem("*","ID=".$id);
        if($res){
            $res['stime'] = strtotime($res['BAD_TIME']) - time();                              //倒计时
            $res['surplus'] = $this->format_price($res['PRICE_ALLOW'] - $res['PRICE_LOAD']);  //剩余金额
            $res['loading'] = round(($res['PRICE_LOAD'] / $res['PRICE_ALLOW'])*100);        //进度
            $res['PRICE_ALLOW'] = $this->format_price($res['PRICE_ALLOW']);        //格式化金额
        }
        //用户余额
        if($this->user){
            $usermoney = $this->lft_money($this->user['USERNAME']);
            T::$tpl->set('usermoney', $usermoney);
        }


        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
        $deal_stat = self::$conf->confGroup("deal_stat");
        T::$tpl->set("deal_stat", $deal_stat);

        T::$tpl->set('res', $res);
    }
    public function actionDoBid(){
        T::$tplFile='public/empty';

        $res = array();
        $res['code'] = 'error';
        $res['msg'] = '无';

        $id = intval($_REQUEST['id']);
        $dealdata = $this->dealLogic->getDealItem("*","ID=".$id);
        $dealdata['surplus'] = $dealdata['PRICE_ALLOW'] - $dealdata['PRICE_LOAD'];
        if($dealdata['DEAL_STAT']==2 && strtotime($dealdata['BAD_TIME'])>time()){ //判断借款进行中 不超过流标时间
            //用户余额
            if($this->user){
                $usermoney = $this->lft_money($this->user['USERNAME']);
            }

            $loadmoney = $_REQUEST['money'];
            if($usermoney && $usermoney['MONEY']>=$loadmoney){
                if($dealdata['MAX_LOAD']>=$loadmoney && $dealdata['surplus']>=$loadmoney){ //判断金额大小上限
                    //加入投资表
                    $data = array();
                    $data['deal_id'] = $dealdata['ID'];
                    $data['user_id'] = $this->user['ID'];
                    $data['money'] = $loadmoney;
                    $recordid = $this->dealLogic->addDealLoadItem($data);
                    if($recordid){
                        //更新项目
                        $dataarr = array();
                        $dataarr['PRICE_LOAD'] = $dealdata['PRICE_LOAD']+$loadmoney;
                        if($dataarr['PRICE_LOAD']==$dealdata['PRICE_ALLOW']){ 
                            $dataarr['DEAL_STAT'] = 3;  //判断是否满标
                        }
                        $dealupdata = $this->dealLogic->updateDealVal($dataarr,'ID='.$dealdata['ID']);
                        $res['code'] = 'success';
                    }
                }else{
                     $res['msg'] = '超过最大投标限额或剩下可投资金额';
                }
            }else{
                $res['msg'] = '余额不足';
            }
        }else{
            $res['msg'] = '找不到项目ID,可能项目已过期或满标';
        }
        echo $this->getResources($res);
    }
}//End CLass
?>
